from rest_framework import serializers

from polls.models import Subject, Teacher





class TeacherCreatSerialicer(serializers.ModelSerializer):
    '''
    为POST请求定义新的序列化器
    '''

    class Meta:
        model = Teacher
        fields = '__all__'

class TeacherSerializer(serializers.ModelSerializer):
    # 单独处理的属性
    gender = serializers.SerializerMethodField()
    subject = serializers.SerializerMethodField()



    @staticmethod
    def get_gender(teacher):
        return '男' if teacher.gender else '女'

    @staticmethod
    def get_subject(teacher):
        return teacher.subject.name
        # return SubjectSimpleSerializer(teacher.subject).data

    class Meta:
        model = Teacher
        # exclude = ('subject',)
        fields = '__all__'


class SubjectSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Subject
        fields = ('no', 'name')


class SubjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Subject
        fields = '__all__'
